package com.xlh.openai;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.Objects;
import java.util.concurrent.*;

/**
 * @author chufangzheng
 */
@Data
@Accessors(chain = true)
@ApiModel("ProductBookStockDelay")
@Slf4j
@ToString
@AllArgsConstructor
public class ProductBookStockDelay implements Delayed, Runnable{

    @ApiModelProperty("预定库存ID")
    private Long bookStoreId;

    @ApiModelProperty("执行时间")
    private Long second;

    @ApiModelProperty("排序")
    private Integer sort;


    @Override
    public void run() {
        synchronized (second){
            log.info(" ------开始执行预定------ ");
            System.out.println( "预定库存ID:" + bookStoreId);
            System.out.println( "执行时间:" + second);
            System.out.println( "排序:" + sort);
            try {
                // 执行更新数据库操作
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public long getDelay(TimeUnit unit) {
        Long currentSecond = DateUtil.currentSeconds();
        System.out.println(currentSecond);
        return TimeUnit.SECONDS.toSeconds((second -  currentSecond));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ProductBookStockDelay that = (ProductBookStockDelay) o;
        return Objects.equals(bookStoreId, that.bookStoreId) &&
               Objects.equals(second, that.second);
    }

    @Override
    public int hashCode() {
        return Objects.hash(bookStoreId, second);
    }

    @Override
    public int compareTo(Delayed delayed) {
        ProductBookStockDelay productBookStockDelay = (ProductBookStockDelay)delayed;
        if(this.second.compareTo(productBookStockDelay.getSecond()) == 0){
            // 如果时间相等 则比较
            return sort.compareTo(productBookStockDelay.getSort());
        }
        return this.second.compareTo(productBookStockDelay.getSecond());
    }


    public static void main(String[] args) {

        DelayQueue<ProductBookStockDelay> delayDelayQueue = new DelayQueue<>();
        Long time = (System.currentTimeMillis() + 5000L) / 1000;

        delayDelayQueue.add( new ProductBookStockDelay(1L, time, 1 ));
        delayDelayQueue.add( new ProductBookStockDelay(2L, time, 2 ));
        delayDelayQueue.add( new ProductBookStockDelay(3L, time, 3 ));
        delayDelayQueue.add( new ProductBookStockDelay(4L, time, 4 ));
        delayDelayQueue.add( new ProductBookStockDelay(5L, time, 5 ));

        new Thread(()->{
            while(true){
                try {
                    ProductBookStockDelay take = delayDelayQueue.take();
                    take.run();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}
